package com.pizzaria.bean;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import br.com.dk.database.DBActionsBase;
import br.com.dk.database.MMConnection;

public class PedidoDAO extends DBActionsBase{
	public static Pedido get(long id) {
		return findSingle(Pedido.class,  Restrictions.eq("id",id));
	}
	public static  List<Pedido> getByCliente(Cliente o) {
		return find(Pedido.class, Restrictions.eq("cliente", o));
	}
	public static List<Pedido> getAll() {
		return find(Pedido.class, null, Order.desc("data"));
	}
	public static List<Pedido> getLastestPedido(Cliente o){
		MMConnection mmcon = MMConnection.getInstance();
		mmcon.createConnection();
		try{
			Criteria cri = mmcon.getSession().createCriteria(Cliente.class);
			cri.add(Restrictions.eq("cliente", o));
			cri.addOrder(Order.desc("data"));
			cri.setMaxResults(3);
			return cri.list();
		}catch (RuntimeException e) {
			e.printStackTrace();
			return null;
		}finally{
			mmcon.closeConnection();
		}
	}
}